import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import sklearn.linear_model as lm
import sklearn.metrics as sm
import sklearn.model_selection as ms
import sklearn.ensemble as se

data = pd.read_csv('../data_test/bike_day.csv')
data.drop(['instant', 'dteday', 'casual', 'registered'],
          axis=1, inplace=True)

x = data.iloc[:, :-1]
y = data.iloc[:, -1]
train_x, \
test_x, \
train_y, \
test_y = ms.train_test_split(x, y, test_size=0.2, random_state=7)

# 随机森林RF
model = se.RandomForestRegressor(max_depth=4,
                                 n_estimators=1000,
                                 min_samples_split=5)
model.fit(train_x, train_y)
pred_test_y = model.predict(test_x)
pred_train_y = model.predict(train_x)
print('RF训练r2:', sm.r2_score(train_y, pred_train_y))
print('RF测试r2:', sm.r2_score(test_y, pred_test_y))
print(sm.mean_absolute_error(test_y,pred_test_y))